package com.easydev.basic.web.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

/**
 * 拦截器案例
 * 
 * @author glf
 * @version 1.0
 * @CreateDate 2016年3月17日
 */
public class DemoInterceptor implements HandlerInterceptor {

    private Logger log = LoggerFactory.getLogger(DemoInterceptor.class);

    /**
     * 在业务处理器处理请求之前被调用 <br/>
     * 如果返回false 从当前的拦截器往回执行所有拦截器的afterCompletion(),再退出拦截器链 <br/>
     * 如果返回true 执行下一个拦截器,直到所有的拦截器都执行完毕 再执行被拦截的Controller
     * 
     * @author glf
     * @param request
     * @param response
     * @param arg2
     * @return
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object arg2) throws Exception {
        log.info("DemoInterceptor preHandle执行");
        log.info("拦截到的请求是:" + request.getRequestURI());
        return true;
    }

    /**
     * 在业务处理器处理请求执行完成后,生成视图之前执行的动作
     * 
     * @author glf
     * @param request
     * @param response
     * @param arg2
     * @param modelAndView
     * @throws Exception
     */
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object arg2, ModelAndView modelAndView) throws Exception {
        log.info("DemoInterceptor postHandle执行");
    }

    /**
     * 在DispatcherServlet完全处理完请求后被调用<br/>
     * 当有拦截器抛出异常时,会从当前拦截器往回执行所有的拦截器的afterCompletion()
     * 
     * @author glf
     * @param request
     * @param response
     * @param arg2
     * @param exception
     * @throws Exception
     */
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object arg2, Exception exception) throws Exception {
        log.info("DemoInterceptor afterCompletion执行");
    }

}
